//loading zhu18@vip.qq.com 2020-9-21
import { runtimeServer } from 'anov-core'
import loadingOption from './loadingOption'

const apploadingId = 'appLoading'
const apploadingObj = document.getElementById(apploadingId)

const progressNumId = 'loadingNum'
const numDom = document.getElementById(progressNumId)

const progressBarId = 'loadingProgressBar'
const barDom = document.getElementById(progressBarId)

const textId = 'loadingText'
const textDom = document.getElementById(textId)

let loading = {
  ...runtimeServer.getEventBus(),
  state: 'show',
  currentOption: {},
  option: loadingOption,
  time1: 2,
  config: [
    { level: 'L1', time: 0.5, stageNum: 97 },
    { level: 'L2', time: 0.5, stageNum: 97 },
    { level: 'L3', time: 1, stageNum: 95 },
    { level: 'L4', time: 2, stageNum: 90 },
    { level: 'L5', time: 3, stageNum: 90 }
  ],
  currentConfig: {},
  flag: true,
  barTween: null,
  supportHighestLevel: '',
  show() {
    apploadingObj.style.display = ''
    this.state = 'show'
  },
  hide() {
    apploadingObj.style.display = 'none'
    this.state = 'hide'
  },
  /**
   * 根据大屏等级获取当前配置
   */
  _getCurrentOption() {
    let level = __ANOV__ENV.level
    //根据大屏等级设置第二段时间
    for (let i = 0; i < this.config.length; i++) {
      if (this.config[i].level == level) {
        this.currentConfig = this.config[i]
      }
    }
    loadingOption.defaultOption.map((i) => {
      if (i.level == level) {
        this.currentOption = i
        return
      }
    })
  },
  /**
   * 根据当前GPU performance获取可支持的最高大屏等级
   */
  _getSupportHighestLevel() {
    let performance = app.envServer.hardware.GPUInfo?.performance || 0

    if (
      performance >
      loadingOption.defaultOption[loadingOption.defaultOption.length - 1]
        .performance[1]
    ) {
      this.supportHighestLevel =
        loadingOption.defaultOption[
          loadingOption.defaultOption.length - 1
        ].level
      return
    }
    loadingOption.defaultOption.map((i) => {
      let arr = i.performance
      if (performance >= arr[0] && performance < arr[1]) {
        this.supportHighestLevel = i.level
        return
      }
    })
  },
  // 进度条和数字加载
  _loadingBarNum() {
    let _this = this
    let step = Math.ceil(
      this.currentConfig.stageNum / loadingOption.config.length
    )
    this.barTween = TweenMax.to(barDom, this.time1 + this.currentConfig.time, {
      width: '100%',
      onUpdate: function () {
        let str = barDom.style.width.replace('%', '')
        let idx = Math.ceil(str / step)
        if (idx > loadingOption.config.length - 1)
          idx = loadingOption.config.length - 1
        numDom.innerHTML = (str - 0).toFixed(0)
        textDom.innerHTML = loadingOption.config[idx - 1].msg + '...'
        if (str >= _this.currentConfig.stageNum) {
          _this.flag && this.pause()
          _this.flag && (textDom.innerHTML = '系统数据解析...')
        }
      },
      onComplete: () => {
        textDom.innerHTML = '系统加载完成'
        setTimeout(() => {
          this.hide()
          app.anovPage.enter()
        }, 500)
      }
    })
  },
  _toFinished() {
    this.flag = false
    let paused = this.barTween.paused()
    if (paused) {
      this.barTween.paused(false)
    }

    this._getSupportHighestLevel()
  },
  /**
   * 显示loadingwarn
   */
  _showLoadingWarn() {
    let performance = app.envServer.hardware.GPUInfo?.performance
    if (performance < this.currentOption.performance[0]) {
      var loadingWarnDom = document.getElementById('loadingWarning')
      var str = `<span class="loading-colse-btn" id="closeBtn">关闭</span><span class="laoding-ani"></span><a href="${loadingOption.href}" target="_blank" class="loading-warn-text" id="loadingWarnText">经检测，大屏等级为${__ANOV__ENV.level}，在当前系统环境下运行可能会有卡顿或展示效果不完整</a>`
      loadingWarnDom.innerHTML = str
      if (this.currentOption.performance[0] - performance > loadingOption.gap) {
        loadingWarnDom.className = 'loading-warning bad'
        app.envServer.callMutation('setGPULevel', 1)
      } else {
        loadingWarnDom.className = 'loading-warning'
        app.envServer.callMutation('setGPULevel', 2)
      }
      TweenMax.to('#loadingWarning', 0.2, { opacity: 1 })
      TweenMax.fromTo(
        '#loadingWarning',
        0.2,
        { opacity: 0 },
        { opacity: 1, repeat: 1, delay: 0.5 }
      )

      document.getElementById('closeBtn').onclick = function () {
        TweenMax.to('#loadingWarning', 0.2, { opacity: 0 })
      }
    } else {
      app.envServer.callMutation('setGPULevel', 3)
      return
    }
  }
}
loading._getCurrentOption()
loading._loadingBarNum()

export default loading
